package com.sduept.bigdata.iec61850.client;

import com.beanit.iec61850bean.*;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 客户端服务
 *
 * @author: lxj
 * @date: 2022/3/24 8:17
 * @since
 */
@Service
public class ClientService {
    static int serverPort = 102;
//    static String serverAddress = "127.0.0.1";
//    static String filePathName = "E:\\公司文档\\项目管理\\智能录波器\\子站smcd文件\\继保\\smcdmodel_1_2021-07-15-19-53-47.smcd";
    static String serverAddress = "10.140.21.243";
    static String filePathName = "E:\\aaa\\smcdmodel\\山大实验室_20220304_V1.00.scd";

    /**
     * 客户端列表
     */
    private final ConcurrentHashMap<String, ClientHolder> clientSapConcurrentHashMap = new ConcurrentHashMap<>();

    /**
     * 初始化连接，应获取所有需要连接的装置
     */
    @PostConstruct
    public void initServer() {
        ClientHolder clientHolder = new ClientHolder(serverAddress,serverPort,filePathName);
        clientSapConcurrentHashMap.put(serverAddress,clientHolder);
    }
    /**
     * 获取服务端的模型
     * @param serverAddress
     * @return Servermodel
     */
    public ServerModel getModel(String serverAddress) {
        ClientHolder clientHolder = clientSapConcurrentHashMap.get(serverAddress);
        return clientHolder.getModel();
    }

    /**
     * 根据对象地址获取数据集
     *
     * @param serverAddress
     * @param objectReference
     * @return
     * @throws IOException
     * @throws SclParseException
     */
    public DataSet getDataSetValues(String serverAddress,String objectReference) throws IOException, SclParseException {
        ClientHolder clientHolder = clientSapConcurrentHashMap.get(serverAddress);
        return clientHolder.getDataSetValues(objectReference);
    }

    public Collection<DataSet> getDataSets(String serverAddress) {
        ClientHolder clientHolder = clientSapConcurrentHashMap.get(serverAddress);
        return clientHolder.getDataSets();
    }

    public Collection<Urcb> getUrcbs(String serverAddress) {
        ClientHolder clientHolder = clientSapConcurrentHashMap.get(serverAddress);
        return clientHolder.getUrcbs();
    }

    public Collection<Brcb> getBrcbs(String serverAddress) {
        ClientHolder clientHolder = clientSapConcurrentHashMap.get(serverAddress);
        return clientHolder.getBrcbs();
    }

    public Urcb getUrcb(String serverAddress,String objectReference) {
        ClientHolder clientHolder = clientSapConcurrentHashMap.get(serverAddress);
        return clientHolder.getUrcb(objectReference);
    }

    public Brcb getBrcb(String serverAddress,String objectReference) {
        ClientHolder clientHolder = clientSapConcurrentHashMap.get(serverAddress);
        return clientHolder.getBrcb(objectReference);
    }

    public void enableBRcb(String serverAddress,String objectReference) throws ServiceError, IOException, SclParseException {
        ClientHolder clientHolder = clientSapConcurrentHashMap.get(serverAddress);
        clientHolder.enableBRcb(objectReference);
    }

    public void enableURcb(String serverAddress,String objectReference) throws ServiceError, IOException, SclParseException {
        ClientHolder clientHolder = clientSapConcurrentHashMap.get(serverAddress);
        clientHolder.enableURcb(objectReference);
    }

    public void disableURcb(String serverAddress,String objectReference) throws ServiceError, IOException, SclParseException {
        ClientHolder clientHolder = clientSapConcurrentHashMap.get(serverAddress);
        clientHolder.disableURcb(objectReference);
    }

    public void disableBRcb(String serverAddress,String objectReference) throws ServiceError, IOException, SclParseException {
        ClientHolder clientHolder = clientSapConcurrentHashMap.get(serverAddress);
        clientHolder.disableBRcb(objectReference);
    }

    /**
     * 根据引用地址，查找模型节点
     *
     * @param objectReference
     * @param fc
     * @return
     * @throws ServiceError
     * @throws IOException
     */
    public ModelNode findNode(String serverAddress,String objectReference, String fc) throws ServiceError, IOException {
        ClientHolder clientHolder = clientSapConcurrentHashMap.get(serverAddress);
        return clientHolder.findNode(objectReference,fc);
    }
    /**
     * 修改数据对象,不同的数据对象要根据实际要改的类型设置修改值，例如要改的是boolean 类型，Object value 应是Boolean 类型
     *
     * @param objectReference
     * @param fc
     * @param value
     */
    public void setDataValues(String serverAddress,String objectReference, String fc, Object value) throws ServiceError, IOException, SclParseException {
        ClientHolder clientHolder = clientSapConcurrentHashMap.get(serverAddress);
        clientHolder.setDataValues(objectReference,fc,value);

    }

    public List<FileInformation> getFiles(String serverAddress,String fileDirectory) throws ServiceError, IOException {
        ClientHolder clientHolder = clientSapConcurrentHashMap.get(serverAddress);
        return clientHolder.getFiles(fileDirectory);
    }

    public void downloadFile(String serverAddress,String serverFilePathName, String localSavePath) throws ServiceError, IOException {
        ClientHolder clientHolder = clientSapConcurrentHashMap.get(serverAddress);
        clientHolder.downloadFile(serverFilePathName,localSavePath);
    }

    public String selectFcDataObject(String serverAddress,String objectReference,String fc) throws ServiceError, IOException {
        ClientHolder clientHolder = clientSapConcurrentHashMap.get(serverAddress);
        return clientHolder.selectFcDataObject(objectReference,fc);
    }
    public void operate(String serverAddress,String objectReference,String fc) throws ServiceError, IOException {
        ClientHolder clientHolder = clientSapConcurrentHashMap.get(serverAddress);
        clientHolder.operate(objectReference,fc);
    }
}
